package com.quanyan.operative.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.operative.db.*;
import com.quanyan.operative.mapper.*;
import com.quanyan.operative.model.req.ReqOpDynamicCommentItem;
import com.quanyan.operative.model.req.ReqOpDynamicItem;
import com.quanyan.operative.model.resp.*;
import com.quanyan.operative.service.OpActivityService;
import com.quanyan.user.response.RespUserInfoBase;
import com.quanyan.user.serviceFacade.UserServiceFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by slong.guo on 2016/12/23.
 */
@Service
public class OpActivityServiceImpl implements OpActivityService{

    private static final int OP_ACTIVITY_VALID = 1;
    private static final int OP_ACTIVITY_OUTDATED = 0;

    private static final int OP_DYNAMIC_CREATE = 1;
    private static final int OP_DYNAMIC_REVISE = 2;

    private static final int DYN_SORT_NEW = 1;
    private static final int DYN_SORT_LIKE = 2;

    private static final int DEFAULT_PAGE_NUM = 0;
    private static final int DEFAULT_PER_PAGE = 10;
    private static final int DEFAULT_DYN_LIKERS_NUM = 7;

    private static final int DYN_ADD_READ = 1;
    private static final int DYN_ADD_LIKE = 2;


    @Autowired
    private UserServiceFacade userServiceFacade;

    @Autowired
    private TbOpActivityMapper tbOpActivityMapper;

    @Autowired
    private TbOpCommentsMapper tbOpCommentsMapper;

    @Autowired
    private TbOpDynamicsMapper tbOpDynamicsMapper;

    @Autowired
    private TbOpDynamicsLikeMapper tbOpDynamicsLikeMapper;


    @Autowired
    private TbOpRewardInfoMapper tbOpRewardInfoMapper;

    @Override
    public APIResponse<List<RespActivityItem>> getOpActivities() {
        List<RespActivityItem> lst = getCurrentActivities();
        if(lst == null || lst.isEmpty()){
            return APIResponse.returnFail("当前没有活动");
        }
        return APIResponse.returnSuccess(lst);
    }

    @Override
    public APIResponse<RespActivityItem> getActivityInfo(int activityID) {
        RespActivityItem item = getActivityInfoByID(activityID);
        if(item == null){
            return APIResponse.returnFail("没有此活动或此活动当前未开放");
        }
        return APIResponse.returnSuccess(item);
    }

    @Override
    public APIResponse<RespRewardItem> getRewardInfo(int rewardID) {
        RespRewardItem item = getRespRewardItem(tbOpRewardInfoMapper.selectByPrimaryKey(rewardID));
        if(item == null){
            return APIResponse.returnFail("没有此ID对应的礼物模板包");
        }

        return APIResponse.returnSuccess(item);
    }


    @Override
    public APIResponse deleteDynamic(int uid, int dynamicID, int activityID){
        TbOpDynamicsWithBLOBs tbOpDynamicsWithBLOBs = tbOpDynamicsMapper.selectByPrimaryKey(dynamicID);
        if(tbOpDynamicsWithBLOBs == null){
            return APIResponse.returnFail("此动态不存在");
        }

        if(tbOpDynamicsWithBLOBs.getUid() != uid){
            return APIResponse.returnFail("不能删除别人的动态");
        }

        if(tbOpDynamicsWithBLOBs.getActivityId() != activityID){
            return APIResponse.returnFail("活动信息有误");
        }

        tbOpDynamicsWithBLOBs.setIsDelete((byte)1);
        tbOpDynamicsMapper.updateByPrimaryKey(tbOpDynamicsWithBLOBs);
        return APIResponse.returnSuccess();

    }

    @Override
    public APIResponse<RespPostDynamic> postDynamic(int uid, ReqOpDynamicItem reqOpDynamicItem) {
        if(uid <= 0){
            return APIResponse.returnFail("参数错误,UID无效");
        }
        if(getActivityInfoByID(reqOpDynamicItem.getActivityID()) == null){
            return APIResponse.returnFail("参数错误,活动不存在或已过期");
        }

        if(!isValidReqOpDynamicItem(reqOpDynamicItem)){
            return APIResponse.returnFail("请求参数有误");
        }

        TbOpDynamicsWithBLOBs tbOpDynamicsWithBLOBs = new TbOpDynamicsWithBLOBs();
        tbOpDynamicsWithBLOBs.setWords(reqOpDynamicItem.getWords());
        tbOpDynamicsWithBLOBs.setPics(StringUtil.join(",", reqOpDynamicItem.getPics()));


        int dynamicID = reqOpDynamicItem.getDynamicID();

        if(reqOpDynamicItem.getOpType() == OP_DYNAMIC_CREATE){
            APIResponse<RespUserInfoBase> userInfoBase = userServiceFacade.queryUserInfo(uid);
            if(!userInfoBase.isRet()){
                return APIResponse.returnFail("用户信息有误");
            }

            tbOpDynamicsWithBLOBs.setUid(uid);
            tbOpDynamicsWithBLOBs.setUserName(userInfoBase.getData().getNickName());
            tbOpDynamicsWithBLOBs.setImgUrl(userInfoBase.getData().getIcon());
            tbOpDynamicsWithBLOBs.setActivityId(reqOpDynamicItem.getActivityID());

            tbOpDynamicsMapper.insertSelective(tbOpDynamicsWithBLOBs);

            dynamicID = tbOpDynamicsWithBLOBs.getId();

        }else{
            TbOpDynamicsWithBLOBs origItem = tbOpDynamicsMapper.selectByPrimaryKey(reqOpDynamicItem.getDynamicID());
            if(origItem == null){
                return APIResponse.returnFail("操作失败,无法修改不存在的动态");
            }

            tbOpDynamicsWithBLOBs.setId(reqOpDynamicItem.getDynamicID());
            tbOpDynamicsWithBLOBs.setUpdateTime(new Date());
            tbOpDynamicsMapper.updateByPrimaryKeySelective(tbOpDynamicsWithBLOBs);

        }

        TbOpDynamicsWithBLOBs newDynamic = tbOpDynamicsMapper.selectByPrimaryKey(dynamicID);

        RespPostDynamic result = new RespPostDynamic();
        RespDynamicItem item = getRespDynamicItem(uid, newDynamic, DEFAULT_PAGE_NUM, DEFAULT_DYN_LIKERS_NUM);

        result.setDynamic(item);

        return APIResponse.returnSuccess(result);

    }

    private boolean isValidReqOpDynamicItem(ReqOpDynamicItem reqOpDynamicItem){
        if(reqOpDynamicItem == null){
            return false;
        }
        if(reqOpDynamicItem.getOpType() != OP_DYNAMIC_CREATE && reqOpDynamicItem.getOpType() != OP_DYNAMIC_REVISE){
            return false;
        }

        if(reqOpDynamicItem.getWords() == null || reqOpDynamicItem.getWords().isEmpty()){
            return false;
        }

        if(reqOpDynamicItem.getPics() == null || reqOpDynamicItem.getPics().isEmpty()){
            return false;
        }

        return true;

    }

    @Override
    public APIResponse<RespDynamicItem> getDynamicItem(int uid, int activityID, int dynamicID) {
        TbOpDynamicsWithBLOBs tbOpDynamicsWithBLOBs = getDynamicItemByID(dynamicID, activityID);
        if(tbOpDynamicsWithBLOBs == null){
            return APIResponse.returnFail("参数错误,此ID对应的动态不存在或与活动ID不对应");
        }

        tbOpDynamicsWithBLOBs.setViewCnt(tbOpDynamicsWithBLOBs.getViewCnt() + 1);
        tbOpDynamicsMapper.updateByPrimaryKeyWithBLOBs(tbOpDynamicsWithBLOBs);
        RespDynamicItem respDynamicItem = getRespDynamicItem(uid, tbOpDynamicsWithBLOBs, DEFAULT_PAGE_NUM, DEFAULT_DYN_LIKERS_NUM);

        return APIResponse.returnSuccess(respDynamicItem);
    }

    @Override
    public APIResponse<RespDynamicList> getDynamicList(int uid, int activityID, int type, int pageNo, int perPage) {
        if(activityID < 0 || (type != DYN_SORT_NEW && type != DYN_SORT_LIKE) || pageNo < 0 || perPage <= 0){
            return APIResponse.returnFail("参数错误");
        }

        List<TbOpDynamicsWithBLOBs> tbOpDynamicsWithBLOBsList = getDynamicItemListWithOrder(activityID, type, pageNo, perPage);

        if(tbOpDynamicsWithBLOBsList == null){
            return APIResponse.returnFail("暂无更多数据");
        }

        RespDynamicList dynamicList = new RespDynamicList();
        List<RespDynamicItem> respDynamicItemList = new ArrayList<>();

        dynamicList.setPageNo(pageNo);
        dynamicList.setPerPage(perPage);
        dynamicList.setServerTime(new Date());

        for(TbOpDynamicsWithBLOBs item : tbOpDynamicsWithBLOBsList){
            item.setViewCnt(item.getViewCnt() + 1);
            tbOpDynamicsMapper.updateByPrimaryKeyWithBLOBs(item);
            RespDynamicItem respItem = getRespDynamicItem(uid, item, DEFAULT_PAGE_NUM, DEFAULT_DYN_LIKERS_NUM);
            if(respItem != null){
                respDynamicItemList.add(respItem);
            }
        }


        dynamicList.setDynamicList(respDynamicItemList);


        return APIResponse.returnSuccess(dynamicList);

    }

    private RespDynamicItem getRespDynamicItem(int uid, TbOpDynamicsWithBLOBs tbOpDynamicsWithBLOBs, int pageNo, int perPage){
        if(tbOpDynamicsWithBLOBs == null){
            return null;
        }

        RespDynamicItem respDynamicItem = new RespDynamicItem();

        respDynamicItem.setId(tbOpDynamicsWithBLOBs.getId());
        respDynamicItem.setWords(tbOpDynamicsWithBLOBs.getWords());
        respDynamicItem.setPics(Arrays.asList(tbOpDynamicsWithBLOBs.getPics().split(",")));

        RespOpUserInfoItem issuer = new RespOpUserInfoItem();
        issuer.setUid(tbOpDynamicsWithBLOBs.getUid());
        issuer.setUserName(tbOpDynamicsWithBLOBs.getUserName());
        issuer.setImgUrl(tbOpDynamicsWithBLOBs.getImgUrl());
        respDynamicItem.setIssuer(issuer);

        respDynamicItem.setActivityId(tbOpDynamicsWithBLOBs.getActivityId());
        respDynamicItem.setViewCnt(tbOpDynamicsWithBLOBs.getViewCnt());
        respDynamicItem.setLikeCnt(tbOpDynamicsWithBLOBs.getLikeCnt());
        respDynamicItem.setIsLike(0);

        TbOpDynamicsLikeExample example = new TbOpDynamicsLikeExample();
        TbOpDynamicsLikeExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);
        criteria.andDynamicIdEqualTo(tbOpDynamicsWithBLOBs.getId());

        List<TbOpDynamicsLike> lst = tbOpDynamicsLikeMapper.selectByExample(example);
        if(lst != null && !lst.isEmpty()){
            respDynamicItem.setIsLike(lst.get(0).getIsLike());
        }


        respDynamicItem.setServerTime(new Date());
        respDynamicItem.setUpdateTime(tbOpDynamicsWithBLOBs.getUpdateTime());

        if(pageNo >= 0 && perPage > 0) {
            respDynamicItem.setDynamicsLike(getDynamicLikeInfo(respDynamicItem.getId(), pageNo, perPage));
        }

        return respDynamicItem;

    }


    private RespDynamicsLike getDynamicLikeInfo(int dynamicID, int pageNo, int perPage){
        TbOpDynamicsLikeExample example = new TbOpDynamicsLikeExample();
        TbOpDynamicsLikeExample.Criteria criteria = example.createCriteria();

        criteria.andDynamicIdEqualTo(dynamicID);
        criteria.andIsLikeEqualTo((byte)1);

        example.setOrderByClause("update_time desc");
        example.setPage(new Page(pageNo * perPage, perPage));


        List<TbOpDynamicsLike> tbOpDynamicsLikeList = tbOpDynamicsLikeMapper.selectByExample(example);
        List<RespOpUserInfoItem> respOpUserInfoItemList = new ArrayList<>();
        for(TbOpDynamicsLike opDynamicsLike : tbOpDynamicsLikeList){
            respOpUserInfoItemList.add(getOpUserInfoByLikeItem(opDynamicsLike));
        }

        RespDynamicsLike dynamicsLike = new RespDynamicsLike();
        dynamicsLike.setPageNo(pageNo);
        dynamicsLike.setPerPage(perPage);
        dynamicsLike.setUserInfoItemList(respOpUserInfoItemList);

        return dynamicsLike;
    }

    private List<RespOpUserInfoItem> getDynamicLikers(int dynamicID, int activityID, int pageNo, int perPage){
        TbOpDynamicsLikeExample example = new TbOpDynamicsLikeExample();
        TbOpDynamicsLikeExample.Criteria criteria = example.createCriteria();
        criteria.andDynamicIdEqualTo(dynamicID);
        criteria.andActivityIdEqualTo(activityID);
        criteria.andIsLikeEqualTo((byte) 1);

        example.setOrderByClause("update_time desc");
        example.setPage(new Page(pageNo * perPage, perPage));

        List<TbOpDynamicsLike> opDynamicsLikeList = tbOpDynamicsLikeMapper.selectByExample(example);
        List<RespOpUserInfoItem> userInfoItems = new ArrayList<>();
        for(TbOpDynamicsLike item : opDynamicsLikeList){
            userInfoItems.add(getOpUserInfoByLikeItem(item));
        }
        return userInfoItems;
    }

    private RespOpUserInfoItem getOpUserInfoByLikeItem(TbOpDynamicsLike item){
        if(item == null){
            return null;
        }
        RespOpUserInfoItem respItem = new RespOpUserInfoItem();
        respItem.setUid(item.getUid());
        respItem.setUserName(item.getUserName());
        respItem.setImgUrl(item.getImgUrl());
        return respItem;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
    public APIResponse<RespDynamicReadAndLike> addDynamicReadAndLike(int uid, int dynamicID, int activityID, int type) {
        if(type != DYN_ADD_READ && type != DYN_ADD_LIKE || uid < 0 || dynamicID < 0 || activityID < 0){
            return APIResponse.returnFail("参数错误");
        }

        RespActivityItem activityItem = getActivityInfoByID(activityID);
        if(activityItem == null){
            return APIResponse.returnFail("参数错误,活动不存在或已过期");
        }

        TbOpDynamicsWithBLOBs dynamicItem = getDynamicItemByID(dynamicID, activityID);
        if(dynamicItem == null){
            return APIResponse.returnFail("参数错误,此ID对应的动态不存在或与活动ID不对应");
        }

        RespDynamicReadAndLike readAndLike = new RespDynamicReadAndLike();


        if(type == DYN_ADD_LIKE){

            if(uid < 0){
                return APIResponse.returnFail("需登录才能点赞");
            }

            TbOpDynamicsLikeExample example = new TbOpDynamicsLikeExample();
            TbOpDynamicsLikeExample.Criteria criteria = example.createCriteria();
            criteria.andUidEqualTo(uid);
            criteria.andDynamicIdEqualTo(dynamicID);
            criteria.andActivityIdEqualTo(activityID);

            List<TbOpDynamicsLike> opDynamicsLikeList = tbOpDynamicsLikeMapper.selectByExample(example);
            if(opDynamicsLikeList == null || opDynamicsLikeList.isEmpty()){
                APIResponse<RespUserInfoBase> userInfoBase = userServiceFacade.queryUserInfo(uid);
                if(!userInfoBase.isRet()){
                    return APIResponse.returnFail("用户信息有误");
                }

                dynamicItem.setLikeCnt(dynamicItem.getLikeCnt() + 1);

                TbOpDynamicsLike opDynamicsLike = new TbOpDynamicsLike();
                opDynamicsLike.setUid(uid);
                opDynamicsLike.setActivityId(activityID);
                opDynamicsLike.setDynamicId(dynamicID);
                opDynamicsLike.setUserName(userInfoBase.getData().getNickName());
                opDynamicsLike.setImgUrl(userInfoBase.getData().getIcon());
                opDynamicsLike.setIsLike((byte)1);
                readAndLike.setIsLike((byte)1);

                tbOpDynamicsLikeMapper.insertSelective(opDynamicsLike);
            }else{
                TbOpDynamicsLike opDynamicsLike = opDynamicsLikeList.get(0);

                if(opDynamicsLike.getIsLike() == (byte)0){
                    dynamicItem.setLikeCnt(dynamicItem.getLikeCnt() + 1);
                    opDynamicsLike.setIsLike((byte)1);
                    readAndLike.setIsLike((byte)1);
                }else{
                    dynamicItem.setLikeCnt(dynamicItem.getLikeCnt() - 1);
                    opDynamicsLike.setIsLike((byte)0);
                    readAndLike.setIsLike((byte)0);
                }

                opDynamicsLike.setUpdateTime(new Date());

                tbOpDynamicsLikeMapper.updateByPrimaryKeySelective(opDynamicsLike);
            }
        } else{
            dynamicItem.setViewCnt(dynamicItem.getViewCnt() + 1);
        }

        readAndLike.setViewCnt(dynamicItem.getViewCnt());
        readAndLike.setLikeCnt(dynamicItem.getLikeCnt());

        if(type == DYN_ADD_LIKE){
            readAndLike.setLikers(getDynamicLikers(dynamicID, activityID, DEFAULT_PAGE_NUM, DEFAULT_DYN_LIKERS_NUM));
        }else{
            readAndLike.setLikers(new ArrayList<RespOpUserInfoItem>());
        }

        tbOpDynamicsMapper.updateByPrimaryKeyWithBLOBs(dynamicItem);

        return APIResponse.returnSuccess(readAndLike);
    }

    @Override
    public APIResponse<List<RespOpUserInfoItem>> getDynamicLikeList(int dynamicID, int activityID, int pageNo, int perPage){

        return APIResponse.returnSuccess(getDynamicLikers(dynamicID, activityID, pageNo, perPage));
    }


    private RespOpUserInfoItem getRespUserInfoBaseByUID(int uid){
        APIResponse<RespUserInfoBase> userInfoBase = userServiceFacade.queryUserInfo(uid);
        if(!userInfoBase.isRet()){
            return null;
        }

        RespOpUserInfoItem ub = new RespOpUserInfoItem();
        ub.setUid(uid);
        ub.setUserName(userInfoBase.getData().getNickName());
        ub.setImgUrl(userInfoBase.getData().getIcon());

        return ub;
    }

    @Override
    public APIResponse<RespPostComment> postComment(int uid, ReqOpDynamicCommentItem commentItem) {
        if(uid < 0 || commentItem.getContent() == null || commentItem.getContent().isEmpty()){
            return APIResponse.returnFail("参数有误");
        }
        RespActivityItem activityItem = getActivityInfoByID(commentItem.getActivityID());
        if(activityItem == null){
            return APIResponse.returnFail("参数错误,活动不存在或已过期");
        }

        TbOpDynamicsWithBLOBs dynamicItem = getDynamicItemByID(commentItem.getDynamicID(), commentItem.getActivityID());
        if(dynamicItem == null){
            return APIResponse.returnFail("参数错误,此ID对应的动态不存在或与活动ID不对应");
        }

        RespOpUserInfoItem ubSrc = getRespUserInfoBaseByUID(uid);
        if(ubSrc == null){
            return APIResponse.returnFail("用户信息获取失败");
        }

        TbOpComments opComments = new TbOpComments();
        opComments.setContent(commentItem.getContent());
        opComments.setDynamicId(commentItem.getDynamicID());
        opComments.setActivityId(commentItem.getActivityID());

        opComments.setSrcUid(uid);
        opComments.setSrcUserName(ubSrc.getUserName());
        opComments.setSrcImgUrl(ubSrc.getImgUrl());



        opComments.setIsDelete((byte)0);

        RespPostComment respPostComment = new RespPostComment();
        respPostComment.setIsCommentOther(0);

        RespDynamicCommentItem comment = new RespDynamicCommentItem();
        comment.setContent(commentItem.getContent());
        comment.setSrcUID(uid);
        comment.setSrcUserName(ubSrc.getUserName());
        comment.setSrcImgUrl(ubSrc.getImgUrl());


        if(commentItem.getTargetCommentID() > 0 && commentItem.getTargetUID() > 0){

            TbOpComments tgtComment = tbOpCommentsMapper.selectByPrimaryKey(commentItem.getTargetCommentID());
            if(tgtComment == null){
                return APIResponse.returnFail("被回复的评论不存在");
            }
            RespOpUserInfoItem ubTarget = getRespUserInfoBaseByUID(commentItem.getTargetUID());

            if(ubTarget == null){
                return APIResponse.returnFail("被回复的对象不存在");
            }
            opComments.setTargetContent(tgtComment.getContent());
            opComments.setTargetCommentId(commentItem.getTargetCommentID());
            opComments.setTargetUid(commentItem.getTargetUID());
            opComments.setTargetUserName(ubTarget.getUserName());
            opComments.setTargetImgUrl(ubTarget.getImgUrl());

            respPostComment.setIsCommentOther(1);

            comment.setTargetCommentID(commentItem.getTargetCommentID());
            comment.setTargetContent(tgtComment.getContent());
            comment.setTargetUID(commentItem.getTargetUID());
            comment.setTargetUserName(ubTarget.getUserName());
            comment.setTargetImgUrl(ubTarget.getImgUrl());

        }


        dynamicItem.setCommentCnt(dynamicItem.getCommentCnt() + 1);
        tbOpDynamicsMapper.updateByPrimaryKeyWithBLOBs(dynamicItem);

        tbOpCommentsMapper.insertSelective(opComments);

        comment.setSrcCommentID(opComments.getId());
        comment.setCreateTime(new Date());

        respPostComment.setComment(comment);

        return APIResponse.returnSuccess(respPostComment);
    }

    @Override
    public APIResponse deleteComment(int uid, int commentID, int dynamicID, int activityID) {
        if(uid < 0 || commentID < 0 || activityID < 0){
            return APIResponse.returnFail("参数错误");
        }

        TbOpDynamicsWithBLOBs dynamicItem = getDynamicItemByID(dynamicID, activityID);
        if(dynamicItem == null){
            return APIResponse.returnFail("参数错误,此ID对应的动态不存在或与活动ID不对应");
        }

        TbOpComments opComments = tbOpCommentsMapper.selectByPrimaryKey(commentID);
        if(opComments == null){
            return APIResponse.returnFail("评论不存在");
        }

        if(opComments.getSrcUid() != uid){
            return APIResponse.returnFail("不是该评论的作者");
        }

        if(opComments.getDynamicId() != dynamicID || opComments.getActivityId() != activityID){
            return APIResponse.returnFail("参数不一致");
        }

        opComments.setIsDelete((byte)1);
        tbOpCommentsMapper.updateByPrimaryKeySelective(opComments);

        if(dynamicItem.getCommentCnt() > 0){
            dynamicItem.setCommentCnt(dynamicItem.getCommentCnt() - 1);
            tbOpDynamicsMapper.updateByPrimaryKeyWithBLOBs(dynamicItem);
        }

        return APIResponse.returnSuccess();

    }

    @Override
    public APIResponse<RespDynamicCommentItem> getCommentItem(int commentID, int dynamicID, int activityID) {
        TbOpComments opComments = tbOpCommentsMapper.selectByPrimaryKey(commentID);
        if(opComments.getDynamicId() != dynamicID || opComments.getActivityId() != activityID){
            return APIResponse.returnFail("参数有误");
        }

        return APIResponse.returnSuccess(getRespCommentItem(opComments));
    }

    @Override
    public APIResponse<RespDynamicCommentList> getCommentList(int dynamicID, int activityID, int pageNo, int perPage) {

        TbOpDynamicsWithBLOBs dynamicItem = getDynamicItemByID(dynamicID, activityID);
        if(dynamicItem == null){
            return APIResponse.returnFail("参数错误,此ID对应的动态不存在或与活动ID不对应");
        }


        TbOpCommentsExample example = new TbOpCommentsExample();
        TbOpCommentsExample.Criteria criteria = example.createCriteria();

        criteria.andActivityIdEqualTo(activityID);
        criteria.andDynamicIdEqualTo(dynamicID);

        criteria.andIsDeleteEqualTo((byte)0);

        example.setOrderByClause("update_time desc");
        example.setPage(new Page(pageNo * perPage, perPage));


        List<TbOpComments> opCommentsList = tbOpCommentsMapper.selectByExample(example);
        if(opCommentsList == null || opCommentsList.isEmpty()){
            return APIResponse.returnFail("暂无更多数据");
        }

        RespDynamicCommentList respDynamicCommentList = new RespDynamicCommentList();
        List<RespDynamicCommentItem> commentItemList = new ArrayList<>();
        for(TbOpComments item : opCommentsList){
            RespDynamicCommentItem respItem = getRespCommentItem(item);
            if(item != null){
                commentItemList.add(respItem);
            }
        }

        respDynamicCommentList.setTotalNum(dynamicItem.getCommentCnt());
        respDynamicCommentList.setPageNo(pageNo);
        respDynamicCommentList.setPerPage(perPage);

        respDynamicCommentList.setCommentItemList(commentItemList);
        respDynamicCommentList.setServerTime(new Date());

        return APIResponse.returnSuccess(respDynamicCommentList);
    }

    private RespDynamicCommentItem getRespCommentItem(TbOpComments opComments){
        if(opComments == null){
            return null;
        }

        RespDynamicCommentItem respDynamicCommentItem = new RespDynamicCommentItem();
        respDynamicCommentItem.setSrcCommentID(opComments.getId());
        respDynamicCommentItem.setContent(opComments.getContent());
        respDynamicCommentItem.setSrcUID(opComments.getSrcUid());
        respDynamicCommentItem.setSrcUserName(opComments.getSrcUserName());
        respDynamicCommentItem.setSrcImgUrl(opComments.getSrcImgUrl());

        respDynamicCommentItem.setTargetCommentID(opComments.getTargetCommentId());
        respDynamicCommentItem.setTargetContent(opComments.getTargetContent());
        respDynamicCommentItem.setTargetUID(opComments.getTargetUid());
        respDynamicCommentItem.setTargetUserName(opComments.getTargetUserName());
        respDynamicCommentItem.setTargetImgUrl(opComments.getTargetImgUrl());

        respDynamicCommentItem.setCreateTime(opComments.getCreateTime());
        respDynamicCommentItem.setServerTime(new Date());

        return respDynamicCommentItem;
    }

    private TbOpDynamicsWithBLOBs getDynamicItemByID(int dynamicID, int activityID){
        TbOpDynamicsWithBLOBs tbOpDynamicsWithBLOBs = tbOpDynamicsMapper.selectByPrimaryKey(dynamicID);
        if(tbOpDynamicsWithBLOBs == null || tbOpDynamicsWithBLOBs.getActivityId() != activityID){
            return null;
        }
        return tbOpDynamicsWithBLOBs;
    }

    private List<TbOpDynamicsWithBLOBs> getDynamicItemListWithOrder(int activityID, int orderType, int pageNo, int perPage){
        TbOpDynamicsExample example = new TbOpDynamicsExample();
        TbOpDynamicsExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityID);
        criteria.andIsDeleteEqualTo((byte)0);
        if(orderType == DYN_SORT_LIKE){
            example.setOrderByClause("like_cnt desc");
        } else{
            example.setOrderByClause("create_time desc");
        }

        example.setPage(new Page(pageNo * perPage, perPage));

        return tbOpDynamicsMapper.selectByExampleWithBLOBs(example);
    }

    private RespActivityItem getActivityInfoByID(int activityID){
        TbOpActivity tbOpActivity = tbOpActivityMapper.selectByPrimaryKey(activityID);
        return getRespActivityItem(tbOpActivity);

    }

    private List<RespActivityItem> getCurrentActivities(){
        TbOpActivityExample example = new TbOpActivityExample();
        TbOpActivityExample.Criteria criteria = example.createCriteria();
        criteria.andStarttimeLessThan(new Date());
        criteria.andEndtimeGreaterThan(new Date());
        criteria.andIsCloseEqualTo((byte)0);

        List<TbOpActivity> tbOpActivityList = tbOpActivityMapper.selectByExample(example);
        List<RespActivityItem> respActivityItemList = new ArrayList<>();
        for(TbOpActivity tbOpActivity : tbOpActivityList){
            RespActivityItem respActivityItem = getRespActivityItem(tbOpActivity);
            if(respActivityItem != null){
                respActivityItemList.add(respActivityItem);
            }
        }

        return respActivityItemList;
    }

    private RespActivityItem getRespActivityItem(TbOpActivity tbOpActivity){
        if(tbOpActivity == null || tbOpActivity.getIsClose().equals((byte)1) ||
                tbOpActivity.getStarttime().after(new Date()) || tbOpActivity.getEndtime().before(new Date())){
            return null;
        }

        RespActivityItem respActivityItem = new RespActivityItem();
        respActivityItem.setActivityID(tbOpActivity.getId());
        respActivityItem.setTitle(tbOpActivity.getTitle());
        respActivityItem.setDesc(tbOpActivity.getDescription());
        respActivityItem.setCity(tbOpActivity.getCity());
        respActivityItem.setStartTime(tbOpActivity.getStarttime());
        respActivityItem.setEndTime(tbOpActivity.getEndtime());
        respActivityItem.setPoster(tbOpActivity.getPoster());
        respActivityItem.setTag(tbOpActivity.getTag());

        Integer rewardID = tbOpActivity.getPrizeTplId();
        if(rewardID != null){
            respActivityItem.setRespRewardItem(getRespRewardItem(tbOpRewardInfoMapper.selectByPrimaryKey(rewardID)));
        }

        return respActivityItem;
    }

    private RespRewardItem getRespRewardItem(TbOpRewardInfo tbOpRewardInfo){
        if(tbOpRewardInfo == null || tbOpRewardInfo.getIsDelete() == 1){
            return null;
        }

        RespRewardItem respRewardItem = new RespRewardItem();
        respRewardItem.setId(tbOpRewardInfo.getId());
        respRewardItem.setRwName0(tbOpRewardInfo.getRwName0());
        respRewardItem.setRwCnt0(tbOpRewardInfo.getRwCnt0());
        respRewardItem.setRwName1(tbOpRewardInfo.getRwName1());
        respRewardItem.setRwCnt1(tbOpRewardInfo.getRwCnt1());

        respRewardItem.setRwName2(tbOpRewardInfo.getRwName2());
        respRewardItem.setRwCnt2(tbOpRewardInfo.getRwCnt2());
        respRewardItem.setRwName3(tbOpRewardInfo.getRwName3());
        respRewardItem.setRwCnt3(tbOpRewardInfo.getRwCnt3());

        respRewardItem.setRwName4(tbOpRewardInfo.getRwName4());
        respRewardItem.setRwCnt4(tbOpRewardInfo.getRwCnt4());
        respRewardItem.setRwName5(tbOpRewardInfo.getRwName5());
        respRewardItem.setRwCnt5(tbOpRewardInfo.getRwCnt5());

        return respRewardItem;
    }


}
